/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.proescola.dao;

import br.com.proescola.modelo.Aluno;
import br.com.proescola.modelo.Disciplina;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Joao
 */
public class DisciplinaDAO {

    private Connection conexao;
    //private PreparedStatement stmt;

    public DisciplinaDAO() throws SQLException {
        this.conexao = CriaConexao.getConexao(); //Istancia um objeto Connection, que é o parâmetro da classe CriaConexao.java
    }

    public void cadastrar(Disciplina d1) throws SQLException {
        //Prepara a estrutura da Query e declara o objeto PreparedStatement
        String sql = "INSERT INTO disciplina (nm_disciplina, cd_professor_cd, cd_curso_cd) VALUES (?,?,?)";
        PreparedStatement stmt = conexao.prepareStatement(sql);
        String ret = "erro";
        //Seta os valores que vão ser inseridos na Query
        stmt.setString(1, d1.getNome());
        stmt.setInt(2, d1.getIdProfessor());
        stmt.setInt(3, d1.getIdCurso());
        stmt.execute();
        stmt.close();

    }

    public List<Disciplina> getLista() throws SQLException {
        String sql = "SELECT * FROM disciplina";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        ResultSet rs = stmt.executeQuery();

        List<Disciplina> disciplinas = new ArrayList<Disciplina>();
        while (rs.next()) {
            Disciplina d1 = new Disciplina(rs.getInt("cd_disciplina"), rs.getString("nm_disciplina"), rs.getInt("cd_professor_cd"),rs.getInt("cd_curso_cd"));
            disciplinas.add(d1);
        }
        rs.close();
        stmt.close();
        return disciplinas;
    }
    public List<Disciplina> getById(String x) throws SQLException {
        String sql = "SELECT * FROM disciplina WHERE nm_disciplina LIKE ?";
        PreparedStatement stmt = this.conexao.prepareStatement(sql);
        stmt.setString(1, ("%" + x + "%"));
        ResultSet rs = stmt.executeQuery();

        List<Disciplina> disciplinas = new ArrayList<Disciplina>();
        while (rs.next()) {
            Disciplina d1 = new Disciplina(rs.getInt("cd_disciplina"), rs.getString("nm_disciplina"), rs.getInt("cd_professor_cd"),rs.getInt("cd_curso_cd"));
            disciplinas.add(d1);
        }
        rs.close();
        stmt.close();
        return disciplinas;
    }
}
